package com.mtao.impl;




import com.mtao.mapper.SysUserMapper;
import com.mtao.SysUserService;
import com.mtao.entity.SysUser;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    private final PasswordEncoder passwordEncoder;

    @Resource
    private SysUserMapper userMapper;


    @Autowired
    public SysUserServiceImpl(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }


    /**
     * 获取用户列表
     * @return
     */
    @Override
    public List<SysUser> getUser() {
        //处理一些 service 业务逻辑
        List<SysUser> userList = userMapper.getUserList();
        for (SysUser user: userList) {
            user.setPassword("******");
        }
        return  userList;
    }

    /**
     * 通过用户名查询用户
     * @param username
     * @return
     */
    @Override
    public SysUser findByName(String username) {
        SysUser sysUser = userMapper.fimdName(username);
        return sysUser;
    }

    @Override
    public List<SysUser> list() {
        return null;
    }


    /**
     * 创建用户
     * @param sysUser
     */
    @Override
    public void createUser(SysUser sysUser) {
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUpdateTime(LocalDateTime.now());
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        userMapper.create(sysUser);
    }
}
